﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace VLXD.uc
{
    public partial class chitietsanpham : System.Web.UI.UserControl
    {
        protected SanPham_Info pro = null;
        public string gia;
        private int ID = 0;
        public string duongdan;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                duongdan = Request.Url.ToString();

                ID = Int32.Parse(Request.QueryString["ID"].ToString());

                pro = SanPham_Info.SanPham_Chon(ID);
                gia = String.Format("{0:0,0 VND}", pro.GiaHienTai);
                Page.Title = pro.TenSanPham;
                //san pham cung loai
                int IDcategori = pro.IDDanhMuc;
                List<SanPham_Info> list = SanPham_Info.SanPham_ChonSPCungLoai(4, IDcategori, Convert.ToInt32(ID));
                if (list.Count > 0)
                {
                    Repeater1.DataSource = list;
                    Repeater1.DataBind();
                }

            }
            catch (Exception)
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Cart_Temp ct = null;
            if (ID != null)
            {
                ct = new Cart_Temp();
                ct.pro = SanPham_Info.SanPham_Chon(ID);
                ct.soluong = 1;

                if (Session["cart"] == null)
                {
                    List<Cart_Temp> listP = new List<Cart_Temp>();
                    listP.Add(ct);
                    Session["cart"] = listP;
                }
                else
                {
                    List<Cart_Temp> listP2 = new List<Cart_Temp>();
                    listP2 = Session["cart"] as List<Cart_Temp>;
                    if (!Cart_Temp.UpdateTongTien(listP2, ct, 1))
                    {
                        listP2.Add(ct);
                    }
                    Session["cart"] = listP2;
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Sản phẩm đã được thêm vào giỏ hàng của bạn');location.href='default.aspx';", true);

            }
        }
    }
}